﻿/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of UserStore.
	/// </summary>
	public class UserStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		public UserStore(Site site,HtmlWeb client)
		{
			_site = site;
			_client = client;
		}
		
		private IDictionary GetUser(string UserId)
		{
			List<IDictionary> entries = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?aID=" + UserId +  "&action=load_administrator&module=administrators");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			List<IDictionary> categories = new List<IDictionary>();
			List<IDictionary> products = new List<IDictionary>();
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{
					IDictionary User = (IDictionary)json[key];
					entries.Add(User);
				}
			}
			
			return entries[0];
		}
		
		public List<IDictionary> GetUsers()
		{
			List<IDictionary> Users = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_administrators&module=administrators");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary record = (IDictionary)recs[i];
						IDictionary User = GetUser(record["id"].ToString());
						User["accesses"] = GetUserAccesses(record["id"].ToString());
						Users.Add(User);
					}
				}
			}
			
			return Users;
		}
		
		private List<IDictionary> GetUserAccesses(string UserId)
		{
			List<IDictionary> UserAccesses = new List<IDictionary>();
			string url = _site.Url + "/admin/json.php?action=get_accesses&module=administrators&node=root";
			
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			string response = "{records:" + node.InnerText + "}";
			IDictionary json = serialiser.Deserialize<IDictionary>(response);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary UserAccess = (IDictionary)recs[i];
						List<IDictionary> children = new List<IDictionary>();
						
						foreach(string cle in UserAccess.Keys)
						{
							switch(cle)
							{
								case "children":
									object[] recs1 = (object[])UserAccess[cle];
									for(int j = 0;j < recs1.Length;j++)
									{
										IDictionary child = (IDictionary)recs1[j];
										children.Add(child);
									}
									break;								
							}
						}
						
						UserAccess["children"] = children;
						UserAccesses.Add(UserAccess);
					}
				}
			}
			
			return UserAccesses;
		}
	}
}
